package bai.demo.web;

import com.sun.org.apache.xalan.internal.xsltc.dom.SortingIterator;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Admin on 12/22 0022.
 */
public class LoginServlet implements Servlet {
    private ServletConfig servletConfig;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("init...");
        this.servletConfig=servletConfig;
    }

    @Override
    public ServletConfig getServletConfig() {
        System.out.println("getServletConfig....");
        return null;
    }
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("Service.....");
        String username=servletRequest.getParameter("username");
        String password=servletRequest.getParameter("password");
        System.out.println("username:  "+username+"<><><>password:  "+password);
        PrintWriter out=servletResponse.getWriter();
        out.println("username:" +username);
        out.println("password: "+password);
        out.flush();

        HttpServletRequest httpServletRequest= (HttpServletRequest) servletRequest;

        String servletPath=httpServletRequest.getServletPath();
        String s=httpServletRequest.getContextPath();
        String user=httpServletRequest.getParameter("username");
        String method=httpServletRequest.getMethod();
        System.out.println(method);
        System.out.println(servletPath);
        System.out.println(s);
        System.out.println(user);

        ServletContext servletContext=servletConfig.getServletContext();
        String initUser=servletContext.getInitParameter("user");
        String initpassword=servletContext.getInitParameter("password");

        if(username.equals(initUser) && password.equals(initpassword)){
            out.print("Hello: "+username+" 欢迎来到英雄联盟!");
        }else{
            out.print("Sorry"+username+"你可以走了!");
        }


    }

    @Override
    public String getServletInfo() {
        System.out.println("getServletInfo....");
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("destroy.....");
    }

    public LoginServlet(){
        System.out.println("LoginServlet...");
    }
}
